package com.yootk.servlet;

import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;

/**
 * Servlet与表单
 */
public class InputServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8"); // 最早是写在JSP页面的
        resp.setContentType("text/html;charset=UTF-8"); // 如果不设置就出现显示乱码
        String msg = req.getParameter("message"); // 接收请求参数
        PrintWriter out = resp.getWriter(); // 获得了客户端浏览器的输出流
        out.println("<html>");
        out.println("<head>");
        out.println("   <title>Yootk - JavaWEB</title>");
        out.println("</head>");
        out.println("<body>");
        out.println("   <h1>" + msg + "</h1>"); // 输出请求参数
        out.println("</body>");
        out.println("</html>");
        out.close(); // 关闭输出流
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
